﻿namespace WLFramework.Reflection.Metadata
{
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using WLFramework.Data;
    using WLFramework.Reflection;
    using WLFramework.Validation;

    /// <summary>
    /// 属性/字段的元数据信息
    /// </summary>
    public class ModelMemberInfo
    {
        public enum MemberTypes
        {
            Property = 1,
            Field = 2,
        }

        private PropertyInfo _Property = null;
        private FieldInfo _Field = null;
        private MemberTypes _MemberType;
        
        /// <summary>
        /// 列名称
        /// </summary>
        public ColumnAttribute Column { get; set; }

        /// <summary>
        /// 忽略标记
        /// </summary>
        public IgnoreAttribute Ignore { get; set; }

        /// <summary>
        /// 友好名称标记
        /// </summary>
        public DisplayAttribute Display { get; set; }

        /// <summary>
        /// 是否允许为空
        /// </summary>
        public ValidationAttribute Required { get; set; }

        /// <summary>
        /// 表单名称
        /// </summary>
        public string FormName 
        {
            get
            {
                return Display == null || string.IsNullOrEmpty(Display.FormName) ?
                    this.MemberName : Display.FormName;
            }
        }
        
        /// <summary>
        /// 类型
        /// </summary>
        public Type Type
        {
            get
            {
                return this._MemberType == MemberTypes.Property ?
                    this._Property.PropertyType :
                    this._Field.FieldType;
            }
        }

        /// <summary>
        /// 成员名称
        /// </summary>
        public string MemberName
        {
            get
            {
                return this._MemberType == MemberTypes.Property ? this._Property.Name : this._Field.Name;
            }
        }

        /// <summary>
        /// 显示的友好名称，如果没有提供则默认为成员名称
        /// </summary>
        public string DisplayName 
        {
            get 
            {
                return this.Display != null ? this.Display.Display : this.MemberName;
            }
        }

        private List<ValidationAttribute> _attributes = new List<ValidationAttribute>();

        /// <summary>
        /// 验证标记集合
        /// </summary>
        public List<ValidationAttribute> Validations { get { return this._attributes; } }

        /// <summary>
        /// 属性/字段设置委托
        /// </summary>
        public TargetSetter Setter 
        {
            get; internal set; 
        }

        /// <summary>
        /// 属性/字段获取委托
        /// </summary>
        public TargetGetter Getter 
        { 
            get; internal set; 
        }

        /// <summary>
        /// 根据属性创建元数据
        /// </summary>
        /// <param name="property">属性</param>
        /// <returns>元数据信息</returns>
        public static ModelMemberInfo Create(PropertyInfo property)
        {
            return new ModelMemberInfo() 
            {
                _MemberType = MemberTypes.Property,
                _Property = property,
                Getter = property.CreatePropertyGetter(),
                Setter = property.CreatePropertySetter()
            };
        }

        /// <summary>
        /// 根据字段创建元数据
        /// </summary>
        /// <param name="field">字段</param>
        /// <returns>元数据信息</returns>
        public static ModelMemberInfo Create(FieldInfo field)
        {
            return new ModelMemberInfo()
            {
                _MemberType = MemberTypes.Field,
                _Field = field,
                Getter = field.CreateFieldGetter(),
                Setter = field.CreateFieldSetter()
            };
        }
    }
}
